#include<iostream>
#include"HttpServer.hpp"

#define WWWROOT "/home/dyx/Muduo/muduo/HttpServer/wwwroot"

std::string RequestStr(const HttpRequest &req)
{
    std::stringstream ss;
    ss<<req._method<<" "<<req._path<<" "<<req._version<<"\r\n";
    for(auto &param:req._params){
        ss<<param.first<<": "<<param.second<<"\r\n";
    } 
    for(auto &header:req._headers){
        ss<<header.first<<": "<<header.second<<"\r\n";
    }
    ss<<"\r\n";
    ss<<req._body;
    return ss.str();
}
void TestGet(const HttpRequest &req , HttpResponse *resp)
{
    resp->SetContent(RequestStr(req) ,"text/plain");//"text/plain"表示纯文本类型
}
void TestPost(const HttpRequest &req , HttpResponse *resp)
{
    resp->SetContent(RequestStr(req) ,"text/plain");//"text/plain"表示纯文本类型
}
void TestPut(const HttpRequest &req , HttpResponse *resp)
{
    std::string path = WWWROOT + req._path;
    Util::WriteFile(path,req._body);
}
void TestDelete(const HttpRequest &req , HttpResponse *resp)
{
    resp->SetContent(RequestStr(req) ,"text/plain");//"text/plain"表示纯文本类型
}
int main()
{
    HttpServer ser(8888,10);
    ser.SetThreadNum(3);
    ser.SetBaseDir(WWWROOT);//设置静态资源根目录
    ser.SetGET("/TestGet",TestGet);
    ser.SetPOST("/login",TestPost);
    ser.SetPUT("/Test.txt",TestPut);
    ser.SetDELETE("/Test.txt",TestDelete);
    ser.Start();
    return 0;
}


